package pe.intix.drawback.actions

import javax.persistence.*
import org.openxava.actions.*
import org.openxava.jpa.*
import org.openxava.model.*

import static pe.intix.drawback.model.Dua.Estado.*

/**
 * @author Jeromy Altuna
 *
 */
class ProcesarDuaAction extends ViewBaseAction {

	void execute() {
		String result = ""
		String idDua = getView().getValue("idDua")
		
		Query query = XPersistence.getManager()
			.createNativeQuery("exec sp_generar_drawback_dua ?, ?")
		try{
			query.setParameter(1, idDua).setParameter(2, result).executeUpdate()
		}catch(PersistenceException pe){
			String message = pe.getCause().getCause().getMessage() //Específico para SQLServer
			throw new PersistenceException(message)	
		}
		Map<String, Object> values =  new HashMap<>()
		values.put "estado", PROCESADA
		MapFacade.setValues getModelName(), getView().getKeyValues(), values
		resetDescriptionsCache()
		removeActions "Dua.procesarDua"			
		addMessage "dua_processed_correctly", idDua				
	}
}
